import { ListBase } from 'src/components/list_base_abs';
import { Component, Emit, Prop, Provide, Watch } from 'vue-property-decorator'

import { LogConst } from "src/consts/log_const";
import { updateSelection } from 'src/utils/list_utils';
import * as Api from 'src/api/net';

@Component({})
export default class Log extends ListBase {
    queryData: any
    mainData: any
    advData: any
    isAdvShow: Boolean
    activeRouteItemRoute: string


    initData(that: Log) {
        that.myState = this.$store.state.log;
        that.selection = [];
        that.mainData = {};
        that.advData = {};
        that.queryData = {};
        that.isAdvShow = false
    }

    getListData(): void {
        let sendData: any = {}
        sendData["curr_page"] = this.myState.curr_page
        sendData["page_size"] = this.myState.page_size
        WLDispatch(LogConst.DispatchLogList, sendData);
    }

    mounted(): void {
        this.getListData();
    }

    get _activeRouteItemRoute(): string {
        return this.$route.path
    }

    @Watch('_activeRouteItemRoute', { immediate: true })
    onRouteChanged(val: any) {
        if (val == "/log") {
            this.getListData()
        }
    }

    doQuery(isAdv: Boolean): void {
        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key].name
            }
        }

        if (isAdv) {
            for (let key in this.advData) {
                if (this.advData[key]) {
                    obj[key] = this.advData[key]
                    if (key == "date") {
                        obj.start_date = this.advData[key][0]
                        obj.end_date = this.advData[key][1]
                    }
                }
            }
        }

        this.queryData = obj
        obj["curr_page"] = this.myState.curr_page
        obj["page_size"] = this.myState.page_size
        WLDispatch(LogConst.DispatchLogList, obj);
    }


    timestampToTime(timestamp: Date): String {
        //时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var date = new Date(timestamp);
        var Y, M, D, h, m;
        Y = date.getFullYear() + "-";
        M =
            (date.getMonth() + 1 < 10
                ? "0" + (date.getMonth() + 1)
                : date.getMonth() + 1) + "-";
        D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

        h = date.getHours() + ":";
        m = date.getMinutes();
        return Y + M + D + "  " + h + m;
    }

    setName(data: any[]): String {
        var val = ""
        if (isNoEmpty(data)) {
            for (let i = 0; i < data.length; i++) {
                if (i == data.length - 1) {
                    val += data[i].name
                } else {
                    val += data[i].name + ','
                }
            }
        }
        return val
    }

    setDate(date: Date): String {
        return this.timestampToTime(date)
    }

    showAdv(flag: Boolean): void {
        this.isAdvShow = flag
    }

    doDownLoad(): void {
        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key].name
            }
        }

        if (this.isAdvShow) {
            for (let key in this.advData) {
                if (this.advData[key]) {
                    obj[key] = this.advData[key]
                    if (key == "date") {
                        obj.start_date = this.advData[key][0]
                        obj.end_date = this.advData[key][1]
                    }
                }
            }
        }

        obj["curr_page"] = this.myState.curr_page
        obj["page_size"] = this.myState.page_size

        Api.AjaxPost(Config.ajaxUrl + "/webhistory/list.do", obj, (res: any) => {
            if (isNoEmptyObject(res.data)) {
                if (isNoEmpty(res.data.data_list)) {
                    var web_name = '', account_name = '', dept_name = '', start_date = '', end_date = ''
                    if (this.mainData.web_name && this.mainData.web_name.name) {
                        web_name = this.mainData.web_name.name
                    }

                    if (this.mainData.account_name && this.mainData.account_name.name) {
                        account_name = this.mainData.account_name.name
                    }

                    if (this.mainData.dept_name && this.mainData.dept_name.name) {
                        dept_name = this.mainData.dept_name.name
                    }

                    if (this.isAdvShow) {
                        if (this.advData.date && this.advData.date[0]) {
                            start_date = this.advData.date[0]
                        }

                        if (this.advData.date && this.advData.date[1]) {
                            end_date = this.advData.date[1]
                        }
                    }

                    var myDate = new Date()
                    var file_name = "" + myDate.getFullYear() + myDate.getMonth() + myDate.getDate() + myDate.getHours() + myDate.getMinutes() + myDate.getSeconds()
                    window.open(Config.ajaxUrl + "/webhistory/download.do?web_name=" + web_name + "&account_name=" + account_name + "&dept_name=" + dept_name + "&start_date=" + start_date + "&end_date=" + end_date + "&file_name=" + file_name + ".xlsx", "download")
                } else {
                    WLMessage("没有可下载数据", "warning")
                }
            } else {
                WLMessage("没有可下载数据", "warning")
            }
        })
    }


    sizeChange(size: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = this.myState.curr_page
        senData["page_size"] = size
        WLDispatch(LogConst.DispatchLogList, senData);
    }

    pageChange(page: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = page
        senData["page_size"] = this.myState.page_size
        WLDispatch(LogConst.DispatchLogList, senData);
    }

    handleSelectionChange(list: any[]): void {
        updateSelection(this.selection, list);
        console.log("this selection", this.selection, list);
    }


    rowClassName(data: any): string {
        if (data.row.selected) {
            return 'selected_row';
        }
        return '';
    }



}